package com.sky.controller.admin;

import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("adminCategoryController")
@RequestMapping("/admin/category")
@Api(tags = "分类管理")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 新增分类
     *
     * @param dto
     * @return
     */
    @PostMapping
    @ApiOperation(value = "新增分类")
    public Result add(@RequestBody CategoryDTO dto) {

        return categoryService.add(dto);
    }

    /**
     * 分页管理
     *
     * @param dto
     * @return
     */
    @GetMapping("/page")
    @ApiOperation(value = "分页查询")
    public Result page(CategoryPageQueryDTO dto) {
        return categoryService.page(dto);
    }

    /**
     * 根据id删除分类
     *
     * @param id
     * @return
     */
    @DeleteMapping
    @ApiOperation(value = "删除分类")
    public Result deleteById(Long id) {
        return categoryService.deleteById(id);
    }

    /**
     * 根据id查询分类(数据回显)
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询")
    public Result selectById(@PathVariable Long id) {
        return categoryService.selectById(id);
    }


    /**
     * 修改分类
     *
     * @param dto
     * @return
     */
    @PutMapping
    @ApiOperation(value = "修改分类")
    public Result updateCategory(@RequestBody CategoryDTO dto) {

        return categoryService.updateCategory(dto);
    }

    /**
     * 启用禁用
     *
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation(value = "启用禁用")
    public Result updateStatus(@PathVariable Integer status, Long id) {

        return categoryService.updateStatus(status, id);
    }

    /**
     * 根据类型查询分类
     *
     * @param type
     * @return
     */
    @GetMapping("/list")
    @ApiOperation(value = "根据类型查询分类")
    public Result<List<Category>> selectByType(Integer type) {


        return categoryService.selectByType(type);
    }

}
